unit clsTransportadora;

interface

uses Dm, dbtables, biblio, sysutils;

type
  TTransportadora = class(TObject)
  private
    Tran_codigo: string;
  public
    Cid_codigo: string;
    Tran_nome: string;
    Tran_endereco: string;
    Tran_placa: string;
    Tran_ufplaca: string;
    Tran_cgccpf: string;
    Tran_inscest: string;
    Tran_fone: string;
    Tran_datacad: TDateTime;
    Tran_dataina: TDateTime;
    Tran_datacar: TDateTime;
    Tran_volume: Double;
    Tran_obs: string;
    ent_codigo: string;
    hist_codigo: string;
    Cod_Trans: string; //
    Tran_cep: string;
    Tran_Perc_Valor: double;
    Tran_Perc_Peso: double;
    Tran_Email: string;

    function Gravar(p_Tran_codigo: string): Byte;
    function Recuperar(p_Tran_codigo: string): Byte;
    function Recuperar_nome(p_nome: string): Byte;
    function Recuperar_ultimo: string;
    function Excluir(p_Tran_codigo: string): Byte;
    procedure Consultar(var p_q: TQuery; p_nome: string; p_tipo: Byte);
  end;

implementation

uses menu;

function TTransportadora.Gravar;
var
  tq: TQuery;
  xsql: string;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('SELECT * FROM Transportadora where Tran_codigo=:k_Tran_codigo');
  tq.parambyname('k_Tran_codigo').asstring := strzero(p_Tran_codigo, 3);
  tq.open;
  if tq.eof then begin
    tq.sql.clear;
    tq.params.clear;
    xsql := 'INSERT INTO Transportadora VALUES(:k_Tran_codigo,';
    if cid_codigo = '' then xsql := xsql + 'null,' else xsql := xsql + ':k_cid_codigo,';
    xsql := xsql +
      ':k_Tran_nome,' +
      ':k_Tran_endereco,' +
      ':k_Tran_placa,' +
      ':k_Tran_ufplaca,' +
      ':k_Tran_cgccpf,' +
      ':k_Tran_inscest,' +
      ':k_Tran_fone,' +
      ':k_Tran_datacad,' +
      ':k_Tran_dataina,' +
      ':k_Tran_datacar,' +
      ':k_Tran_volume,' +
      ':k_Tran_obs,';
    if ent_codigo <> '' then xsql := xsql + ':K_ent_codigo,' else xsql := xsql + 'null,';
    if hist_codigo <> '' then xsql := xsql + ':K_hist_codigo,' else xsql := xsql + 'null,';
    xsql := xsql + ':k_Tran_cep, :k_Tran_Perc_Valor, :k_Tran_Perc_Peso, :k_Tran_Email)';
    tq.sql.add(xsql);
  end else begin
    tq.sql.clear;
    tq.params.clear;
    xsql := 'UPDATE Transportadora SET ';
    if cid_codigo = '' then xsql := xsql + 'cid_codigo=null,' else xsql := xsql + 'Cid_codigo   =:k_Cid_codigo,';
    xsql := xsql +
      'Tran_nome    =:k_Tran_nome    ,' +
      'Tran_endereco=:k_Tran_endereco,' +
      'Tran_placa   =:k_Tran_placa   ,' +
      'Tran_ufplaca =:k_Tran_ufplaca ,' +
      'Tran_cgccpf  =:k_Tran_cgccpf  ,' +
      'Tran_inscest =:k_Tran_inscest ,' +
      'Tran_fone    =:k_Tran_fone    ,' +
      'Tran_datacad =:k_Tran_datacad ,' +
      'Tran_dataina =:k_Tran_dataina ,' +
      'Tran_datacar =:k_Tran_datacar ,' +
      'Tran_volume  =:k_Tran_volume  ,' +
      'Tran_obs     =:k_Tran_obs     ,';
    if ent_codigo <> '' then xsql := xsql + 'ent_codigo=:K_ent_codigo,' else xsql := xsql + 'ent_codigo=null,';
    if hist_codigo <> '' then xsql := xsql + 'hist_codigo=:K_hist_codigo,' else xsql := xsql + 'hist_codigo=null,';
    xsql := xsql + 'Tran_cep=:k_Tran_cep, Tran_Perc_Valor=:k_Tran_Perc_Valor, Tran_Perc_Peso=:k_Tran_Perc_Peso, Tran_Email =:k_Tran_Email '+
    ' WHERE Tran_codigo=:k_Tran_codigo ';
    tq.sql.add(xsql);
  end;
  tq.parambyname('k_Tran_codigo').asstring := strzero(p_Tran_codigo, 3);
  if cid_codigo <> '' then
    tq.parambyname('k_Cid_codigo').asstring := strzero(Cid_codigo, 4);
  tq.parambyname('k_Tran_nome').asstring := Tran_nome;
  tq.parambyname('k_Tran_endereco').asstring := Tran_endereco;
  tq.parambyname('k_Tran_placa').asstring := Tran_placa;
  tq.parambyname('k_Tran_ufplaca').asstring := Tran_ufplaca;
  tq.parambyname('k_Tran_cgccpf').asstring := Tran_cgccpf;
  tq.parambyname('k_Tran_inscest').asstring := Tran_inscest;
  tq.parambyname('k_Tran_fone').asstring := Tran_fone;
  tq.parambyname('k_Tran_datacad').asdatetime := Tran_datacad;
  tq.parambyname('k_Tran_dataina').asdatetime := Tran_dataina;
  tq.parambyname('k_Tran_datacar').asdatetime := Tran_datacar;
  tq.parambyname('k_Tran_volume').asfloat := Tran_volume;
  if tran_obs = '' then Tran_obs := ' ';
  tq.parambyname('k_Tran_obs').asblob := Tran_obs;
  if ent_codigo <> '' then
    tq.parambyname('k_Ent_codigo').asstring := ent_codigo;
  if hist_codigo <> '' then
    tq.parambyname('k_Hist_codigo').asstring := hist_codigo;
  tq.ParamByName('k_Tran_Cep').AsString := Tran_cep;
  tq.ParamByName('k_Tran_Perc_Valor').AsFloat:= Tran_Perc_Valor;
  tq.ParamByName('k_Tran_Perc_Peso').AsFloat:= Tran_Perc_Peso;
  tq.ParamByName('k_Tran_Email').AsString:= Tran_Email;
  tq.close;
  tq.execsql;
  result := 1;
  tq.close;
  tq.Free;
end;

function TTransportadora.Recuperar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM Transportadora where Tran_codigo=:k_Tran_codigo');
  tq.Params[0].asstring := strzero(p_Tran_codigo, 3);
  tq.open;
  Tran_codigo := p_Tran_codigo;
  if tq.eof then begin
    Cid_codigo := '';
    Tran_nome := '';
    Tran_endereco := '';
    Tran_placa := '';
    Tran_ufplaca := '';
    Tran_cgccpf := '';
    Tran_inscest := '';
    Tran_fone := '';
    Tran_datacad := date;
    Tran_dataina := 0;
    Tran_datacar := 0;
    Tran_volume := 0;
    Tran_obs := '';
    ent_codigo := '';
    hist_codigo := '';
    Tran_cep := '';
    Tran_Perc_Valor:= 0;
    Tran_Perc_Peso:= 0;
    Tran_Email:= '';
    Result := 0;
  end else begin
    Cid_codigo := tq.fieldbyname('Cid_codigo').asstring;
    Tran_nome := tq.fieldbyname('Tran_nome').asstring;
    Tran_endereco := tq.fieldbyname('Tran_endereco').asstring;
    Tran_placa := tq.fieldbyname('Tran_placa').asstring;
    Tran_ufplaca := tq.fieldbyname('Tran_ufplaca').asstring;
    Tran_cgccpf := tq.fieldbyname('Tran_cgccpf').asstring;
    Tran_inscest := tq.fieldbyname('Tran_inscest').asstring;
    Tran_fone := tq.fieldbyname('Tran_fone').asstring;
    Tran_datacad := tq.fieldbyname('Tran_datacad').asdatetime;
    Tran_dataina := tq.fieldbyname('Tran_dataina').asdatetime;
    Tran_datacar := tq.fieldbyname('Tran_datacar').asdatetime;
    Tran_volume := tq.fieldbyname('Tran_volume').asfloat;
    Tran_obs := tq.fieldbyname('Tran_obs').asstring;
    ent_codigo := tq.fieldbyname('ent_codigo').asstring;
    hist_codigo := tq.fieldbyname('hist_codigo').asstring;
    tran_cep := tq.fieldbyname('tran_cep').AsString;
    Tran_Perc_Valor:= tq.FieldByName('Tran_Perc_Valor').AsFloat;
    Tran_Perc_Peso:= tq.FieldByName('Tran_Perc_Peso').AsFloat;
    Tran_Email:= tq.FieldByName('Tran_Email').AsString;
    Result := 1;
  end;
  tq.Close;
  tq.Free;
end;

function TTransportadora.Excluir;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('DELETE FROM Transportadora WHERE Tran_codigo=:k_Tran_codigo');
  tq.parambyname('k_Tran_codigo').asstring := strzero(p_Tran_codigo, 3);
  tq.execSQL;
  Result := 1;
  tq.close;
  tq.Free;
end;

procedure TTransportadora.Consultar;
begin
  p_q.sql.Clear;
  p_q.Params.Clear;
  if p_tipo = 0 then begin
    if FileExists(caminho_personal + 'Metal') then begin
      p_q.sql.add('Select * from Transportadora where Tran_nome like :k_Tran_nome order by Tran_nome');
      p_q.parambyname('k_Tran_nome').asstring := '%' + p_nome + '%';
    end else begin
      p_q.sql.add('Select * from Transportadora where Tran_nome>=:k_Tran_nome order by Tran_nome');
      p_q.parambyname('k_Tran_nome').asstring := p_nome;
    end;
  end else begin
    p_q.sql.add('Select * from Transportadora where replace(replace(replace(replace(TRAN_CGCCPF,''.'',''''),'','',''''),''-'',''''),''/'','''') like :k_Tran_CGCCPF ' +
      'order by replace(replace(replace(replace(TRAN_CGCCPF,''.'',''''),'','',''''),''-'',''''),''/'','''')');
    p_q.parambyname('k_Tran_CGCCPF').asstring := p_nome + '%';
  end;
end;

function TTransportadora.Recuperar_ultimo: string;
var
  tq: TQuery;
begin
  tq := TQuery.create(DmInt.Db);
  tq.DatabaseName := DmInt.Db.DatabaseName;
  tq.Active := false;
  tq.sql.clear;
  tq.Params.clear;
  tq.sql.add('Select max(tran_codigo) as maior from transportadora');
  tq.open;
  if tq.fieldbyname('maior').AsString <> '' then
    Result := inttostr(strtoint(tq.fieldbyname('maior').Asstring) + 1)
  else
    Result := '1';
  tq.close;
  tq.free;
end;

function TTransportadora.Recuperar_nome(p_nome: string): Byte;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Select * from Transportadora where Tran_Nome =' + #39 + p_nome + #39);
  tq.Open;
  if tq.Eof then
    Result := 0
  else begin
    Result := 1;
    Cod_Trans := tq.FieldByName('Tran_codigo').AsString;
  end;
  tq.close;
  tq.free;
end;

end.

